package com.kcn.jsonaction;

import java.util.Collections;
import java.util.List;
import com.kcn.dbservices.CurdService;
import com.opensymphony.xwork2.ActionSupport;

public class JqGridBaseAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	// Json返回数据，对应JsonReader的root，用于的到实际数据数组名称
	private List gridModel = Collections.emptyList();
	// 对应prmNames的rows，每页显示的记录行数
	private Integer rowsbypage = 0;
	// 对应prmNames,Jsonreader的page，当前页码
	private Integer currentpage = 0;
	// 对应Jsonreader的total，返回总页数
	private int totalpages = 0;
	// 对应JsonReader的records，返回总记录数
	private int totalrecords = 0;
	// 对应prmNames的order，排序方式
	private String sord;
	// 对应prmNames的sort，用于排序的列名
	private String sidx;
	// 对应prmNames的search，是否是用于查询的请求
	private Boolean search;
	private String searchField;
	private String searchOper;
	private String searchString;
	
	CurdService a;
	@Override
	public String execute() {
		// TODO Auto-generated method stub
		System.out.println(this.getRowsbypage()+"  "+this.getCurrentpage());
		a=new CurdService();
		System.out.println(this.getRowsbypage()+"  "+this.getCurrentpage());
		System.out.println(this.getSearchOper());
		if(this.search){
			String sql="";
			if(this.getSearchOper().equals("bw"))
				sql="from Customer where "+this.searchField+" like '"+this.getSearchString()+"%'";
			else if(this.getSearchOper().equals("eq"))
				sql="from Customer where "+this.searchField+"='"+this.getSearchString()+"'";
			else if(this.getSearchOper().equals("ne"))
				sql="from Customer where "+this.searchField+"<>'"+this.getSearchString()+"'";
			else if(this.getSearchOper().equals("cn"))
				sql="from Customer where "+this.searchField+" like '%"+this.getSearchString()+"%'";
			else
				sql="from Customer";
			System.out.println("******************************************");
			System.out.println(sql);
			return this.refreshGridModel(sql);
		}
		else
		{
			return this.refreshGridModel("from Customer");
		}
	}

	public List Resultsbypage(int from, int length, String sql) {
		return a.getbypage(from, length, sql);
	}

	public String refreshGridModel(String SQL) {
		try {
			List results = Collections.emptyList();
			System.out.println("开始RefreshGrid22");
			totalrecords = a.getTotal(SQL);
			System.out.println(totalrecords);
			int from = rowsbypage * (currentpage - 1);
			int lenth = rowsbypage;

			results = this.Resultsbypage(from, lenth, SQL);
			this.setGridModel(results);
			totalpages = (int) Math.ceil((double) totalrecords / (double) rowsbypage);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionError(e.getMessage());
			return ERROR;
		}
	}

	public List getGridModel() {
		return gridModel;
	}

	public void setGridModel(List gridModel) {
		this.gridModel = gridModel;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public Boolean getSearch() {
		return search;
	}

	public void setSearch(Boolean search) {
		this.search = search;
	}

	public Integer getRowsbypage() {
		return rowsbypage;
	}

	public void setRowsbypage(Integer rowsbypage) {
		this.rowsbypage = rowsbypage;
	}

	public Integer getCurrentpage() {
		return currentpage;
	}

	public void setCurrentpage(Integer currentpage) {
		this.currentpage = currentpage;
	}

	public int getTotalpages() {
		return totalpages;
	}

	public void setTotalpages(int totalpages) {
		this.totalpages = totalpages;
	}

	public int getTotalrecords() {
		return totalrecords;
	}

	public void setTotalrecords(int totalrecords) {
		this.totalrecords = totalrecords;
	}

	public String getSearchField() {
		return searchField;
	}

	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	public String getSearchOper() {
		return searchOper;
	}

	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}
}
